package com.example.zwh.computershop.particulars;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.zwh.computershop.Home_Activity;
import com.example.zwh.computershop.Myapp;
import com.example.zwh.computershop.R;
import com.example.zwh.computershop.handleDataClient.handleData;
import com.example.zwh.computershop.login.Login;
import com.example.zwh.computershop.sqlModel.Cart;
import com.example.zwh.computershop.sqlModel.Product;
import com.example.zwh.computershop.sqlModel.User;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class particulars extends Activity {

    @BindView(R.id.img_part_goback)
    LinearLayout imgPartGoback;
    @BindView(R.id.tv_part_gohome)
    LinearLayout tvPartGohome;
    @BindView(R.id.part_banner_imgs)
    Banner partBannerImgs;
    @BindView(R.id.tv_part_price)
    TextView tvPartPrice;
    @BindView(R.id.tv_part_name)
    TextView tvPartName;
    @BindView(R.id.tv_part_detail)
    TextView tvPartDetail;
    @BindView(R.id.tv_part_addcar)
    TextView tvPartAddcar;
    @BindView(R.id.part_part_xiangqing)
    ImageView partPartXiangqing;
    @BindView(R.id.part_sub_add)
    CicleAddAndSubView partSubAdd;
    @BindView(R.id.part_shopcar_lay)
    LinearLayout partShopcarLay;

    //全部的数据
    private ArrayList<Product> mproducts = new ArrayList<>();
    //商品详情的内容
    private Product product;
    //传到商品详情的String
    private String come_product;
    //商品数量
    private int part_num = 1;
    //banner中的数据
    private List<String> imageTitle_home_top = new ArrayList<>();//标题
    private List<String> imageName_home_top = new ArrayList<>();//图片
    private static int state;
    private Myapp myapp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_particulars);
        ButterKnife.bind(this);
        myapp = (Myapp) getApplication();//拿到myapp了。
        if (getIntent().getStringExtra("come_product") != null) {
            come_product = getIntent().getStringExtra("come_product");
        } else {
            come_product = "华硕";//默认设置数据
        }
        getPro();
        if (state == 3) {
//            Log.e("Shenyutian", "网络数据获取成功：下面选择:"+come_product);
            product = select(come_product);
            initData();
        }
    }

    @OnClick({R.id.img_part_goback, R.id.tv_part_gohome, R.id.tv_part_addcar , R.id.part_shopcar_lay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_part_goback:
//                Toast.makeText(this, "返回", Toast.LENGTH_SHORT).show();
                onStop();
                finish();
                break;
            case R.id.tv_part_gohome:
                startActivity(new Intent(this, Home_Activity.class));//点击返回首页
//                Toast.makeText(this, "返回首页", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_part_addcar:
                Toast.makeText(this, "加入购物车", Toast.LENGTH_SHORT).show();
                AddCar();
                break;
            case R.id.part_shopcar_lay:
//                Toast.makeText(this,"点击购物车",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this, Home_Activity.class);
                intent.putExtra("select",2);//选择视图2,购物车；
                startActivity(intent);//点击返回购物车
                break;
        }
    }

    //导入数据到布局
    private void initData() {
        tvPartPrice.setText("$" + Double.toString(product.getPrice()));
        tvPartDetail.setText(product.getProductInfor());
        tvPartName.setText(product.getProductName());
        setListener();
        initbanner();
    }

    //加载banner轮播图数据
    private void initbanner() {

        imageTitle_home_top.add(product.getProductName()+1);
        imageTitle_home_top.add(product.getProductName()+2);
        imageTitle_home_top.add(product.getProductName()+3);
        imageTitle_home_top.add(product.getProductName()+4);
        if(product.getProductName().contains("联想")) {
            imageName_home_top.add("https://p2.lefile.cn/product/adminweb/2018/10/22/TwhzuMYcyuO6XXrIQvromLFu3-0689.w520.jpg");
            imageName_home_top.add("https://p3.lefile.cn/product/adminweb/2018/05/25/VjcdHraUP4fpjhOwfJ1LV0v76-6834.w520.jpg");
            imageName_home_top.add("https://p2.lefile.cn/product/adminweb/2018/05/25/9XS7m6YBCBGMKEsJYFmZgaVEP-0254.w520.jpg");
            imageName_home_top.add("https://p1.lefile.cn/product/adminweb/2018/05/25/3UomabcgPBws4KrjmI5TTE7do-3596.w520.jpg");
        } else {
            imageName_home_top.add("https://static.asus.com.cn/store/20180912/17455854544956.jpg");
            imageName_home_top.add("https://static.asus.com.cn/store/20180912/17453198485456.jpg");
            imageName_home_top.add("https://static.asus.com.cn/store/20180912/17452250995055.jpg");
            imageName_home_top.add("https://static.asus.com.cn/store/20180912/17450448979910.jpg");
        }
        //设置banner样式
        partBannerImgs.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        //设置图片加载器
        partBannerImgs.setImageLoader(new GlideImageLoader());
        //设置图片集合
        partBannerImgs.setImages(imageName_home_top);
        //设置banner动画效果
        partBannerImgs.setBannerAnimation(Transformer.RotateDown);
        //设置标题集合（当banner样式有显示title时）
        partBannerImgs.setBannerTitles(imageTitle_home_top);
        //设置轮播时间
        partBannerImgs.setDelayTime(3000);
        //设置指示器位置（当banner模式中有指示器时）
        partBannerImgs.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        partBannerImgs.start();
    }

    //加载图片banner轮播图图片
    private class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            //Glide 加载图片简单用法
            Glide.with(context).load((String) path).into(imageView);
        }
    }

    //服务器拿数据了
    public void getPro() {
        Thread t = new Thread(new takeThread());
        t.start();
        try {
            t.join();
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }
    }

    //数据线程
    class takeThread extends Thread {
        @Override
        public void run() {
            state = handleData.getPro(mproducts);//拿数据的ArrayList类型数组mproducts
            //if 就成功了
            if (state == 3) {

            }
        }
    }

    //查询 并将结果存在商品详情product中 可以根据商品idhe商品名称
    public Product select(String come_product) {
        //创建迭代器
//        Log.d("Shenyutian", "select: " + come_product);
        Iterator<Product> productIterator = mproducts.iterator();
        while (productIterator.hasNext()) {
            Product mproduct = productIterator.next();
            if (mproduct.getProductName().contains(come_product) || mproduct.getProductId().contains(come_product)) {
                return mproduct;
            }
        }
        return mproducts.get(1);
    }

    //加入购物车逻辑
    private void AddCar() {
        Cart add_part = new Cart();//存放加入购物车信息
        User user = new User();//存放用户信息
        int user_id = 0;//用户id  默认为0
        if (myapp.getLoState() == 0) {
            Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
            //下面进行登录界面的跳转
            startActivity(new Intent(this, Login.class));
        } else {
            user = myapp.getUser();
            user_id = user.getId();
        }
        add_part.setId(user_id);//用户id在哪里拿？
        add_part.setProductId(product.getProductId());//加入商品id
        add_part.setAddNum(part_num);//商品数量
//        Log.d("Shenyutian", "AddCar:part_num: "+part_num);
        handleData.addCart(add_part);

        //刷新本地购物车数据
//        Log.d("Shenyutian", "cars.size:"+myapp.getCart().size());
        ArrayList<Cart> cars = new ArrayList<>();
        cars.add(add_part);
        myapp.setCart(cars);
//        Log.d("Shenyutian", "cars.size:"+myapp.getCart().size());

    }

    //这是加减的回调
    protected void setListener() {
        partSubAdd.setOnNumChangeListener(new CicleAddAndSubView.OnNumChangeListener() {
            @Override
            public void onNumChange(View view, int stype, int num) {
//                Log.e("Shenyutian", "num: "+num );
                part_num = num;
            }
        });
    }

}
